package com.xdhl.fu.member.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xdhl.fu.common.core.domain.R;
import com.xdhl.fu.common.core.utils.JwtUtils;
import com.xdhl.fu.common.core.utils.PageUtils;
import com.xdhl.fu.common.core.utils.StringUtils;
import com.xdhl.fu.common.core.web.controller.BaseController;
import com.xdhl.fu.common.core.web.domain.AjaxResult;
import com.xdhl.fu.common.core.web.page.TableDataInfo;
import com.xdhl.fu.common.log.annotation.Log;
import com.xdhl.fu.common.log.enums.BusinessType;
import com.xdhl.fu.common.security.annotation.InnerAuth;
import com.xdhl.fu.common.security.annotation.RequiresPermissions;
import com.xdhl.fu.member.domain.MemberBasic;
import com.xdhl.fu.member.service.IMemberBasicService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

/**
 * 会员基本信息控制层
 *
 * @author 朱力维
 * @time 2022/5/27 16:39
 */
@RestController
@RequestMapping("/basic")
public class MemberBasicController extends BaseController {

    @Resource
    private IMemberBasicService memberBasicService;

    /**
     * 查询固定年龄段用户列表
     */
    @InnerAuth
    @GetMapping("/list/age")
    public TableDataInfo listByAge(Integer min, Integer max, Integer pageNumber, Integer pageSize) {
        PageUtils.startPage(pageNumber, pageSize);
        LambdaQueryWrapper<MemberBasic> wrapper = new LambdaQueryWrapper<>();
        LocalDate now = LocalDate.now();
        wrapper.between(MemberBasic::getBirthday, now.minusYears(max), now.minusYears(min));
        List<MemberBasic> list = memberBasicService.list(wrapper);
        return getDataTable(list);
    }

    /**
     * 查询指定职业的用户列表
     */
    @InnerAuth
    @GetMapping("/list/occupation")
    public TableDataInfo listByOccupation(String occupation, Integer pageNumber, Integer pageSize) {
        PageUtils.startPage(pageNumber, pageSize);
        LambdaQueryWrapper<MemberBasic> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MemberBasic::getOccupationClass, occupation);
        List<MemberBasic> list = memberBasicService.list(wrapper);
        return getDataTable(list);
    }

    /**
     * 查询n个月内活跃度用户列表
     */
    @InnerAuth
    @GetMapping("/list/active")
    public TableDataInfo listActive(Integer month, Integer pageNumber, Integer pageSize) {
        PageUtils.startPage(pageNumber, pageSize);
        List<MemberBasic> list = memberBasicService.listActive(month);
        return getDataTable(list);
    }

    /**
     * 按条件查询列表
     */
    @RequiresPermissions("member:basic:list")
    @GetMapping("/list")
    public TableDataInfo list(MemberBasic memberBasic) {
        startPage();
        LambdaQueryWrapper<MemberBasic> lqw = new LambdaQueryWrapper<MemberBasic>();
        String tel = memberBasic.getTelephoneNumber();
        lqw.eq(StringUtils.isNotBlank(tel), MemberBasic::getTelephoneNumber, tel);
        String email = memberBasic.getEmail();
        lqw.eq(StringUtils.isNotBlank(email), MemberBasic::getEmail, email);
        List<MemberBasic> list = memberBasicService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 根据Id查询详细信息
     */
    @RequiresPermissions("member:basic:query")
    @GetMapping(value = "/{dataId}")
    public AjaxResult getInfo(@PathVariable("dataId") String dataId) {
        System.out.println(memberBasicService.getById(dataId));
        return AjaxResult.success(memberBasicService.getById(dataId));
    }

    /**
     * 根据Id查询详细信息
     */
    @InnerAuth
    @GetMapping(value = "/mnumber/{dataId}")
    public R<MemberBasic> getMNumberByDataId(@PathVariable("dataId") String dataId) {
        return R.ok(memberBasicService.getById(dataId));
    }

    /**
     * 根据token查询会员数据Id并返回
     */
    @InnerAuth
    @GetMapping(value = "/data-id-self")
    public R<String> getDataIdSelf(@RequestParam("token") String token) {
        String username = JwtUtils.getUserName(token);
        LambdaQueryWrapper<MemberBasic> condition = new LambdaQueryWrapper<>();
        condition.eq(MemberBasic::getIdCardNumber, username)
                .or().eq(MemberBasic::getEmail, username)
                .or().eq(MemberBasic::getTelephoneNumber, username);
        condition.select(MemberBasic::getDataId);
        String dataId = memberBasicService.getOne(condition).getDataId();
        return R.ok(dataId);
    }

    /**
     * 修改会员信息
     */
    @RequiresPermissions("member:basic:edit")
    @Log(title = "修改会员信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MemberBasic memberBasic) {
        return toAjax(memberBasicService.updateById(memberBasic));
    }

    /**
     * 删除/批量删除会员信息
     */
    @RequiresPermissions("member:basic:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dataIds}")
    public AjaxResult remove(@PathVariable String[] dataIds) {
        return toAjax(memberBasicService.removeByIds(Arrays.asList(dataIds)));
    }
}
